<template>
  <Modal v-model="modal" :title="title" @on-cancel="onCancel">
    <div class="tag-modal h400 scroll" >
      <Form :label-width="140">
        <FormItem label="参与排行的活动标签:">
          <Tree
            class="father-tree"
            :data="data3"
            show-checkbox
            @on-check-change="onChange	"
            multiple
          ></Tree>
        </FormItem>
      </Form>
    </div>
    <div slot="footer">
      <Button @click="cancel" size="large" type="text">取消</Button>
      <Button :disabled="btnDisabled" @click="onSave" type="primary" size="large">确定</Button>
    </div>
  </Modal>
</template>
<script>
import table from "@/table";
import { formatMonth } from "@/util";
export default {
  data() {
    return {
      id: null,
      modal: false,
      title: "排行榜标签设置",
      rankTag: [],
      hasRankTagData: [],
      data3: [
        {
          title: "选择活动标签",
          expand: true,
          children: []
        }
      ],
      btnDisabled: false,
      flag: false
    };
  },
  created() {
    // this.getData();
    this.getEditHasData();
  },
  methods: {
    openModal() {
      this.modal = true;
    },
    onCancel() {
      this.modal = false;
    },
    cancel() {
      this.modal = false;
    },
    onChange(value) {
      this.rankTag = value
        .filter(e => {
          return e.checked && e.id;
        })
        .map(e => {
          return e.id;
        });
    },

    getEditHasData() {
      this.$jsonPost("/report/rank/setting/factor/tagList").then(data => {
        this.data3[0].children = data.map(item => {
          return {
            title: item.name,
            id: item.id,
            checked: item.isCheck
          };
        });
      });
    },
    onSave() {
      if (this.btnDisabled) return;
      this.btnDisabled = true;
      if (!this.rankTag.length) {
        this.$Notice.success({
          title: `保存成功`
        });
        this.btnDisabled = false;
        this.onCancel();
      } else {
        this.rankTag = this.rankTag.filter(item => {
          return item != undefined;
        });
        let param = {
          rankSetKeyEnum: "ACTIVITY_TAG_SET",
          rankSetValue: this.rankTag.join(",")
        };
        let url = "/report/rank/setting/factor/saveOrUpdate";

        this.$jsonPost(url, param)
          .then(data => {
            this.btnDisabled = false;

            this.$Notice.success({
              title: `操作成功`
            });
            this.$emit("complete");
            this.onCancel();
          })
          .catch(() => {
            this.btnDisabled = false;
          });
      }
    }
  }
};
</script>
<style >
.father-tree .ivu-tree-children li {
  margin: 0 0;
}
</style>